public class 环形链表 {

    /**
     * https://leetcode.cn/problems/linked-list-cycle/?envType=study-plan-v2&envId=top-interview-150
     * 快慢双指针
     * @param head
     * @return
     */

    public boolean hasCycle(ListNode head) {
        if(head == null||head.next == null){
            return false;
        }
        ListNode fast = head;
        ListNode last = head;
        do{
            fast = fast.next.next;
            last = last.next;
        }while(fast != null && fast.next != null && fast != last);
        return fast != last ? false : true;
    }
}
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
}
